package com.cell.api.application.netty.utils;

/**
 * <p>
 * 转化十六进制工具类
 * </p>
 *
 * @author zdy
 * @date 2021/11/6
 */
public class ConvertCodeUtils {


    /**
     * 字节数组转16进制字符串
     *
     * @param b 字节数组
     * @return 16进制字符串
     */
    public static String bytes2HexString(byte[] b) {
        StringBuffer result = new StringBuffer();
        String hex = "";
        for (byte value : b) {
            hex = Integer.toHexString(value & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
        }
        return result.append(hex.toUpperCase()).toString();
    }

    /**
     * 16进制字符串转字节数组
     *
     * @param src 16进制字符串
     * @return 字节数组
     */
    public static byte[] hexString2Bytes(String src) {
        int l = src.length() / 2;
        byte[] ret = new byte[l];
        for (int i = 0; i < l; i++) {
            ret[i] = (byte) Integer.valueOf(src.substring(i * 2, i * 2 + 2), 16).byteValue();
        }
        return ret;
    }


}
